﻿using Jermaine.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Jermaine.Extensions
{
    public static class DictionaryExtensions
    {
        /// <summary>
        /// 过滤获取满足条件的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dict"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static IDictionary<string, T> Filter<T>(this IDictionary<string, T> dict, string searchPattern) {
            var clonedDict = Cloning.DeepCopy<IDictionary<string, T>>(dict);
            dict.Clear();
            var regex = new Regex(searchPattern);
            foreach (var item in clonedDict) {
                if (regex.IsMatch(item.Key)) {
                    dict.Add(item.Key, item.Value);
                }
            }
            return dict;
        }

        /// <summary>
        /// 过滤获取满足条件
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <param name="Func"></param>
        /// <returns></returns>
        public static IDictionary<TKey, TValue> Filter<TKey, TValue>(this IDictionary<TKey, TValue> dict, Func<TKey, bool> Func) {
            var clonedDict = Cloning.DeepCopy<IDictionary<TKey, TValue>>(dict);
            dict.Clear();
            foreach (var item in clonedDict) {
                if (Func(item.Key)) {
                    dict.Add(item.Key, item.Value);
                }
            }
            return dict;
        }

        /// <summary>
        /// 过滤获取满足条件
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <param name="Func"></param>
        /// <returns></returns>
        public static IDictionary<TKey, TValue> Filter<TKey, TValue>(this IDictionary<TKey, TValue> dict, Func<TValue, bool> Func) {
            var clonedDict = Cloning.DeepCopy<IDictionary<TKey, TValue>>(dict);
            dict.Clear();
            foreach (var item in clonedDict) {
                if (Func(item.Value)) {
                    dict.Add(item.Key, item.Value);
                }
            }
            return dict;
        }

        /// <summary>
        /// 对Key和Value都做改变
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <param name="KeyFunc"></param>
        /// <param name="ValueFunc"></param>
        /// <returns></returns>
        public static IDictionary<TKey, TValue> Lambda<TKey, TValue>(this IDictionary<TKey, TValue> dict, Func<TKey, TKey> KeyFunc, Func<TValue, TValue> ValueFunc) {
            var clonedDict = Cloning.DeepCopy<IDictionary<TKey, TValue>>(dict);
            dict.Clear();
            foreach (var item in clonedDict) {
                var newKey = KeyFunc(item.Key);
                var newValue = ValueFunc(item.Value);
                dict.Add(newKey, newValue);
            }
            return dict;
        }

        /// <summary>
        /// 对Key做些改变
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <param name="KeyFunc"></param>
        /// <returns></returns>
        public static IDictionary<TKey, TValue> Lambda<TKey, TValue>(this IDictionary<TKey, TValue> dict, Func<TKey, TKey> KeyFunc) {
            var clonedDict = Cloning.DeepCopy<IDictionary<TKey, TValue>>(dict);
            dict.Clear();
            foreach (var item in clonedDict) {
                var newKey = KeyFunc(item.Key);
                dict.Add(newKey, item.Value);
            }
            return dict;
        }

        /// <summary>
        /// 对Value做些改变
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict"></param>
        /// <param name="ValueFunc"></param>
        /// <returns></returns>
        public static IDictionary<TKey, TValue> Lambda<TKey, TValue>(this IDictionary<TKey, TValue> dict, Func<TValue, TValue> ValueFunc) {
            var clonedDict = Cloning.DeepCopy<IDictionary<TKey, TValue>>(dict);
            dict.Clear();
            foreach (var item in clonedDict) {
                var newValue = ValueFunc(item.Value);
                dict.Add(item.Key, newValue);
            }
            return dict;
        }


        public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dict, Action<TKey, TValue> Action) {
            foreach (var item in dict) {
                Action(item.Key, item.Value);
            }
        }
    }
}
